package enumsingleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

public class Test {

    public static void main(String[] args) {
        testByReflect();
    }

    private static void testBySerial() {
        EnumSingleton instance = EnumSingleton.getInstance();
        instance.setData(new Object());
        EnumSingleton instance1 = null;
        try{
            FileOutputStream fos = new FileOutputStream("EnumSingleton.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(instance);
            oos.flush();
            oos.close();

            FileInputStream fileInputStream = new FileInputStream("EnumSingleton.obj");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            instance1 = (EnumSingleton)objectInputStream.readObject();
            System.out.println(instance.getData());
            System.out.println(instance1.getData());
            System.out.println(instance.getData() == instance1.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void testByReflect() {
        try {
            Class<EnumSingleton> enumSingletonClass = EnumSingleton.class;

            Constructor<EnumSingleton> declaredConstructor = enumSingletonClass.getDeclaredConstructor(null);
            declaredConstructor.setAccessible(true);
            EnumSingleton enumSingleton = declaredConstructor.newInstance(null);
            System.out.println(enumSingleton == EnumSingleton.getInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
